(function(w , d){

    var scrollWarpper = function(el , options) {
            var me = this, i;

        this.pullDown = d.getElementById('pullDown');
        this.pullUp = d.getElementById('pullUp');

            me.options = {
                onBeforUpDownFlat: true, //上下开关

                x: 0,
                y: 0,

                startY: 0,
                startX: 0,

                pulldownOffsetHeight:parseInt('-'+this.pullDown.offsetHeight),
                pullupOffsetHeight:parseInt('-'+this.pullUp.offsetHeight),

                transX:0,
                transY:0,
                onscrollBar:false,
                touchmoveiniz:'下拉刷新',
                touchmovetxt : '松开手开始刷新',
                touchenddowntxt :'刷新中...',
                touchendupiniz:'上拉加载更多',
                touchenduptxt:'加载中...',
                touchendupnulltxt : '没有更多数据了',

                toRoll : 0,
                docHeight:0,
                soAreaHeight:0,
                surplusHeight:0,

                touchmoveDownFn : function(args){},
                touchmoveUpFn:function(args){}
            }
            for (i in options) {
                me.options[i] = options[i];
            }

            this.options.transY = this.options.pulldownOffsetHeight;

            this.moveScrollOnOff = false;
            this.warpper = typeof el == 'object' ? el : d.getElementById(el);
            this.warpper.style.display = 'block';
            this.warpper.style.overflow = 'hidden';
            this.nextWarpper = this.warpper.getElementsByTagName('div')[0];
            this.elTransform = this.nextWarpper.style.webkitTransform = 'translate('+this.options.transX+'px, '+this.options.transY+'px) scale(1) translateZ(0px)';
            this.nextWarpper.style.webkitTransitionDuration='500ms';
            this.nextWarpper.style.webkitTransitionProperty = 'transform';
            this.nextWarpper.style.webkitTransformOrigin='0px 0px 0px';

            me.options.toRoll = $(document).scrollTop();
            me.options.docHeight = $(document).height();
            me.options.soAreaHeight = $(window).height();
            me.options.surplusHeight = me.options.docHeight - me.options.toRoll - me.options.soAreaHeight;

            !this.options.onscrollBar ? document.body.style.overflowY ="hidden" : document.body.style.overflowY = 'auto';

            this.START_EV = 'touchstart',
                this.MOVE_EV = 'touchmove',
                this.END_EV = 'touchend',
                this.START_CLICK = 'click';

            //绑定事件
        me._bind(this.START_EV);
        me._bind(this.END_EV);
        me._bind(this.MOVE_EV);
    }

    scrollWarpper.prototype={
        handleEvent: function (e) {
            var me = this;
            switch(e.type) {
                case this.MOVE_EV: me._move(e); break;
                case this.END_EV:me._end(e);break;
                case this.START_EV:me._start(e);break;
            }
        },
        _bind: function (type, el, bubble) {
            (el || this.nextWarpper).addEventListener(type, this, !!bubble);
        },
        _start:function(event){
            var e = e || event;
            e.preventDefault();
            if(e.targetTouches.length > 0){
                var touch = e.targetTouches[0];
                this.options.startY = touch.pageY;
                this.options.startX = touch.pageX;
            }
            this.moveScrollOnOff = true;
        },
        _move:function(event){
            //if(this.moveScrollOnOff) {
                event.preventDefault();
                var touch = event.targetTouches[0];
                this.options.x = touch.pageX;
                this.options.y = touch.pageY;
                var elTransform = this.elTransform;

                var transResult = elTransform.substring(elTransform.indexOf('(') + 1, elTransform.length - 1);
                var transResultArr = transResult.split(',');
                this.options.transX = parseInt(transResultArr[0].replace(/[a-z]{2}/, ''));
                this.options.transY = parseInt(transResultArr[1].replace(/[a-z]{2}/, ''));
                //下拉刷新
                if (this.options.startY < this.options.y) {
                    this.options.transY = this.options.transY + (this.options.y - this.options.startY);

                    if (this.options.transY >= 0) {
                        this.pullDown.innerText = this.options.touchmovetxt;
                    }
                }
                else if (this.options.startY > this.options.y) {
                    if (this.options.surplusHeight == 0) {

                    } else {
                        this.options.transY = parseInt('-' + (Math.abs(this.options.transY) + Math.abs(this.options.y - this.options.startY)));
                    }

                }

                this.refresh();
                this.moveScrollOnOff = false;
            //}
        },
        _end:function(){
            this.moveScrollOnOff = true;
            if(this.options.startY < this.options.y) {
                if (this.options.transY >= 0) {
                    this.options.transY = 0;
                    this.pullDown.innerText = this.options.touchenddowntxt;
                    this.refresh();
                    this.options.touchmoveDownFn(this);
                } else {
                    this.options.transY = this.options.pulldownOffsetHeight;
                    this.refresh();
                }
            }
            else if(this.options.startY > this.options.y){

            }
        },
        refresh:function(){
            this.elTransform = this.nextWarpper.style.webkitTransform = 'translate('+this.options.transX+'px, '+this.options.transY+'px) scale(1) translateZ(0px)';
        }
    }
    w._Scroll = {
        init:function(el,options){
            if(el) {
                this.init.prototype = new scrollWarpper(el, options);
            }
        }
    }
})(window ,document);